package moudle;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import bean.FileListBean;


/**
 * Created by eyesee on 2017/4/25.
 */

public class FileManagerMoudle {

    /**获取扫描出来的文件信息的javaben*/
    public List<FileListBean> getDataBean(){

        List<FileListBean> filesInfolist= new ArrayList<FileListBean>();
        File file = getPath();
        File[] files = file.listFiles();
        FileListBean infoBean;
        for (File item : files) {
            infoBean = new FileListBean();
            infoBean.setName(item.getName());
            infoBean.setLastmodified(item.lastModified());
            infoBean.setBitmap(getVideoBitmap(item));
            filesInfolist.add(infoBean);
        }
        return filesInfolist;
    }

    /**获取扫描出来的文件*/
    public List<java.io.File> getDataFile(){
        List<java.io.File> list = new ArrayList<>();
        File file = getPath();
        File[] files = file.listFiles();
        if (files!=null){
            for (File item : files) {
                list.add(item);
            }
        }else {

        }
        return list;
    }

    //获取sd卡路径
    public File getPath(){
        File sdDir = null;
        //判断sd卡是否存在
        boolean sdDirExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdDirExist){
            //获取根目录
            sdDir = Environment.getExternalStorageDirectory();
        }
        return sdDir;
    }

    /**计算sdcard剩余容量和总容量*/
    public List<String> getsize(){
        //存储剩余容量和总容量信息的集合,下标0为总容量,下标1为已用容量
        List<String> sdSizeInfo = new ArrayList<>();

        //是否插入内存
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            java.io.File path = getPath();
            //文件路径
            StatFs statFs = new StatFs(path.getPath());
            //block的size
            long blockSize = statFs.getBlockSizeLong();
            //block的数量
            long totalBlocks = statFs.getBlockCountLong();
            //已用block数量
            long availableBlock = statFs.getAvailableBlocksLong();
            String[] total = filesize(totalBlocks * blockSize);         //总容量大小
            String[] use = filesize(availableBlock * blockSize);        //已使用容量

            String used = use[0]+use[1];
            String all = total[0]+total[1];

            sdSizeInfo.add(0,used);
            sdSizeInfo.add(1,all);
        }
        return sdSizeInfo;
    }
    //返回数组,下标1代表大小,2代表单位 kB/MB
    public String[] filesize(long size){
        String str="";
        if (size>=1024){
            str = "KB";
            size /= 1024;

            if (size >=1024){
                str = "MB";
                size /= 1024;
            }
        }

        DecimalFormat formatter = new DecimalFormat();
        formatter.setGroupingSize(3);
        String[] result = new String[2];
        result[0] = formatter.format(size);
        result[1] = str;
        return result;
    }

    //获取视频缩略图
    public Bitmap getVideoBitmap(File file){
        Bitmap bitmap =null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(file.getPath());
        bitmap = retriever.getFrameAtTime();

        retriever.release();
        return bitmap;
    }


    /**时间排序*/
    public void getTimeSort(){
        java.io.File path = getPath();
        java.io.File[] files = path.listFiles();
        List<FileListBean> fileInfoList = new ArrayList<FileListBean>();
        for (int i = 0; i < files.length; i++) {
            java.io.File file = files[i];
            FileListBean fileInfo = new FileListBean();

            fileInfo.name = file.getName();
            fileInfo.lastmodified = file.lastModified();

            fileInfoList.add(fileInfo);
        }
        Collections.sort(fileInfoList,new FileComparator());
    }

    //此处泛型为存储扫描文件信息的javabean
    public class FileComparator implements Comparator<FileListBean> {

        @Override
        public int compare(FileListBean info1, FileListBean info2) {
            if (info1.lastmodified < info2.lastmodified){
                return -1;
            }else {
                return 1;
            }
        }


        /*public FileFilter mFileFilter = new FileFilter() {
            @Override
            public boolean accept(FileListBean file) {
                String tmp = file.getName().toLowerCase();
                if (tmp.endsWith(".mov")||tmp.endsWith(".jpg")){
                    return true;
                }
                return false;
            }
        };*/
    }

}
